package DAO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import model.Machine;
import model.Order;
import model.Repair;
import model.SparePart;
import model.StorageArea;
import model.User;
import model.Order.OrderLine;
import model.Repair.RepairLine;

public class Dao {
	private List<SparePart> parts = new ArrayList<SparePart>();
	private List<Machine> machines = new ArrayList<Machine>();
	private List<Repair> repairs = new ArrayList<Repair>();
	private List<StorageArea> drawers = new ArrayList<StorageArea>();
	private List<Order> orders = new ArrayList<Order>();
	//private Map<String, String> users = new HashMap<String, String>();
	private List<User> holders = new ArrayList<User>();

//	----------------------------------------------------------------------------

	public List<SparePart> returnParts() {
		return parts;
	}

	public List<Machine> returnMachines() {
		return machines;
	}

	public List<Repair> returnRepairs() {
		return repairs;
	}

	public List<StorageArea> returnDrawers() {
		return drawers;
	}

	public List<Order> returnOrders() {
		return orders;
	}

	//--------------------------------------------------------------------------

	public void createMachine(int code, String model, String manufacturer,
			String manual, int sketch) {
		Machine mach = new Machine(code, model, manufacturer, manual, sketch);
		machines.add(mach);
	}

	public void createSparePart(int ID, StorageArea drawer, String sketch,
			int minimumAmount, int actualAmount) {
		SparePart part = new SparePart(ID, drawer, sketch);
		part.setMinimumAmount(minimumAmount);
		part.setAmount(actualAmount);
		parts.add(part);
	}

	public void createRepair(Machine machine) {
		//Check the machine state
		Repair repair = new Repair(machine);
		repairs.add(repair);
	}

	public void createStorage(int id) {
		drawers.add(new StorageArea(id));
	}

	public void createOrder(String title) {
		orders.add(new Order(title));
	}

	//--------------------------------------------------------------------------

	/**
	 * Delete a Machine from the Service.
	 * 	Requires: Machine != null 
	 * @author George
	 */
	public void deleteMachine(Machine m) {
		if (machines.contains(m)) {
			machines.remove(m);
		}
	}

	/**
	 * Delete a SparePart from the Service.
	 * 	Requires: SparePart != null 
	 * @author George
	 */
	public void deleteSparePart(SparePart sp) {
		if (parts.contains(sp)) {
			parts.remove(sp);
		}
	}

	public void removeRepair(Repair r) {
		if (repairs.contains(r)) {
			repairs.remove(r);
		}
	}

	/**
	 * Removes a StorageArea from the Service.
	 * 	Requires: StorageArea != null 
	 * @author George
	 */
	public void removeStorage(StorageArea sa) {
		if (drawers.contains(sa)) {
			drawers.remove(sa);
		}
	}

	/**
	 * Removes a Order from the Service.
	 * 	Requires: Order != null 
	 * @author George
	 */
	public void removeOrder(Order o) {
		if (orders.contains(o)) {
			orders.remove(o);
		}
	}

	//----------------------------------------------------------------------------
	
	/**
	 * @author Bogdan
	 * Connects a spare part type to a machine.
	 */
	public void addSparePartToMachine(SparePart part,Machine machine) {
		machine.addSparePart(part);
	}

	//--------------------------------------------------------------------------

	/**
	 * Should register the used spare part it to the repair.
	 * @author George
	 */
	public void registerRepairLine(SparePart part, int amount, Repair repair) {
		repair.createRepairLine(part, amount);
	}

	/**
	 * Should register the order it to the Orderline.
	 * @author George
	 */
	public void registerOrderLine(SparePart part, int amount, Order order) {
		order.createOrderLine(amount, part);
	}

	//-------------------------------------------------------------------------

	/**
	 * The updateOrderLine must update one specific Orderline for one specific Order.
	 * @author Group
	 */
	public void updateOrderLine(SparePart part, int amount, Order order,
			OrderLine orderLine) {
		order.updateOrderLine(part, amount, orderLine);
	}

	/**
	 * The updateSparePart must be able to update the spare parts.
	 * @author Group
	 */
	public void updateSparePart(int idNumber, StorageArea drawer,
			String sketch, int minimumAmount, int actualAmount,
			SparePart sparePart) {
		sparePart.updateSparePart(idNumber, drawer, sketch, minimumAmount,
				actualAmount);
	}

	/**
	 * The updateMachine must be able to update the Machine information.
	 * @author Group
	 */
	public void updateMachine(int code, String model, String manufacturer,
			String manualLocation, int sketch, Machine machine) {
		machine
				.updateMachine(code, model, manufacturer, manualLocation,
						sketch);
	}

	/**
	 * The updateRepair must be able to update one specific machine in the Repair.
	 * @author Group
	 */
	public void updateRepair(Machine machine, Repair repair) {
		repair.updateRepair(machine);
	}

	/**
	 * The updateRepairLine must update one specific RepairLine for one specific Repair.
	 * @author Group
	 */
	public void updateRepairLine(SparePart part, int amount, Repair repair,
			RepairLine repairLine) {
		repair.updateRepairLine(part, amount, repairLine);
	}

	/**
	 * The updateOrder must update the Order.
	 * @author Group
	 */
	public void updateOrder(String title, Order order) {
		order.updateOrder(title);
	}

	/**
	 * The updateStorageArea must update the StorageArea.
	 * @author Group
	 */
	public void updateStorageArea(int id, StorageArea storage) {
		storage.updateStorageArea(id);
	}

	//---------------------------------------------------------------------

	/**
	 * Search for specific seven digit number in the Spare Part list.
	 * </br> If the number is not found return null.
	 * @author George 
	 */
	public SparePart searchSparePart(int idNumber) {
		SparePart part = null;
		for (SparePart sp : parts) {
			if (sp.getIDnumber() == idNumber) {
				part = sp;
				break;
			}
		}
		System.out.println(part);
		return part;
	}

	/**
	 * <center>Search for specific seven digit number in the Machine list .
	 * </br>If the number is not found return null.</center>
	 * @author George 
	 */
	public Machine searchMachine(int code) {
		Machine machin = null;
		for (Machine machine : machines) {
			if (machine.getCode() == code) {
				machin = machine;
				break;
			}
		}
		System.out.println(machines);
		return machin;
	}

	//-------------------------------------------------------------------------
	public void changePassword(String user, String pass) {
//		for (Entry<String, String> e : users.entrySet()) {
//			if (e.getKey().equals(user)) {
//				e.setValue(pass);
//			}
			for (User u : holders) {
				if(u.getUserName().equals(user)) {
					u.setPassword(pass);
				}
			}
		}

	public boolean login (String user, String pass){
		boolean temp = false;
//		for (Entry<String, String> e : users.entrySet()) {
//			if (e.getKey().equals(user)) {
//				if(e.getValue().equals(pass)) temp= true;
//			}
//		}
		for (User u : holders){
			if(u.getUserName().equals(user)) {
				if (u.getPassword().equals(pass)) temp = true;
			}
		}
		return temp;
	}
	
	public void createAccount(String username, String password){
		//users.put(username, password);
		holders.add(new User(username, password));
	}
	
}

//-----------------------------------------------------------------------
//NO UPDATES AND SEARCHES

//-----------------------------------------------------------------------

//-----------------------------------------------------------------------

